//
//  WBHomeViewController.swift
//  传智微博
//
//  Created by FYXJ（6） on 2018/8/27.
//  Copyright © 2018年 FYXJ（6）. All rights reserved.
//

import UIKit

private let cellId = "cellId"

class WBHomeViewController: WBBaseViewController {

    private lazy var listViewModel = WBStatusListViewModel()
    
    override func loadData() {
        print("最后一条信息微博的ID\(String(describing: listViewModel.statusList.last?.id))")
        print("最后一条信息:\(String(describing: listViewModel.statusList.last?.text))")
        listViewModel.loadStatus (pullUP: self.isPullup) { (isSuccess) in
            
            if isSuccess
            {
                self.isPullup = false
                self.tableView?.reloadData()
                self.refreshControl?.endRefreshing()
            }
            if !self.isPullup
            {
                self.isPullup = false
            }
        }
//        DispatchQueue.main.asyncAfter(deadline: DispatchTime.now()+1) {
//            for i in 0..<15 {
//                if self.isPullup {
//                    self.statusList.append("上拉 \(i)")
//                }
//                else
//                {
//                self.statusList.insert(i.description, at: 0)
//                }
//            }
//          
//        }
       
    }
    /// 显示好友
    @objc private func showFriends()  {
//        print(#function)
        let vc = WBDemoViewController()
        navigationController?.pushViewController(vc, animated: true )
    }
    
}

extension WBHomeViewController {
    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return listViewModel.statusList.count
    }
    
    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
       let cell = tableView.dequeueReusableCell(withIdentifier: cellId, for: indexPath)
        cell.textLabel?.text = listViewModel.statusList[indexPath.row].text
        return cell
    }
}
// MARK: - 设置界面
extension WBHomeViewController {
    
    override func setupTableView() {
  
        super.setupTableView()
//        let btn: UIButton = UIButton.cz_textButton("好友", fontSize: 16, normalColor: UIColor.darkGray, highlightedColor: UIColor.orange)
//        btn.addTarget(self, action: #selector(showFriends), for: .touchUpInside)
//        navigationItem.leftBarButtonItem = UIBarButtonItem(customView: btn)
        navItem.leftBarButtonItem = UIBarButtonItem(title: "好友", target: self, action: #selector(showFriends))
//        navigationItem.leftBarButtonItem = UIBarButtonItem(title: "好友", style: .plain, target: self, action:#selector(showFriends))
        
        tableView?.register(UITableViewCell.self, forCellReuseIdentifier: cellId)
    }
}
